package com.raven.play.datalink.test.jdbc;

import akka.actor.ActorSystem;
import akka.stream.javadsl.Keep;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
import java.util.concurrent.CompletionStage;
import java.util.stream.IntStream;

public class AkkaStreamsExample {

    public static void main(String[] args) {
        final ActorSystem system = ActorSystem.create("exampleSystem");

        Source<Integer, CompletionStage<String>> source = Source.fromMaterializer((materializer, attributes) -> {
            // 这里可以放置一些在流启动时需要执行的逻辑
            String materializedValue = "Stream started";

            return Source.from(() -> IntStream.range(1, 100).boxed().iterator())
                    .mapMaterializedValue(ignored -> materializedValue);
        });

        CompletionStage<String> materializedValue = source
                .toMat(Sink.foreach(System.out::println), Keep.left())
                .run(system);

        materializedValue.thenAccept(value -> System.out.println("Materialized Value: " + value));
    }
}
